<?php
namespace app\index\controller;

use think\Controller;
use Db;

class Explain extends Base
{

    //落地页
    public function index()
    {

        $type = input('type', 1);

        $row = Db::name('explain')->where(['type' => $type, 'status' => 1])->order('id desc')->find();


        if (!empty($row['id'])) {

            //点击人数

            $look = Db::name('explain_look_record')->where(['uid' => $this->uid, 'eid' => $row['id']])->find();

            if (!$look) {

                Db::name('explain')->where('id', $row['id'])->setInc('hits', 1);

                $addData = [
                    'eid' => $row['id'],
                    'uid' => $this->uid,
                    'ip' => ip2long(request()->ip())
                ];

                Db::name('explain_look_record')->insert($addData);
            }
        }

        $this->assign('row', $row);

//        if ($type == 2) {
//
//            //申请讲师状态
//            $reply = Db::name('lecturer')->field('status')->where('uid', $this->uid)->find();
//
//            $this->assign('reply', $reply);
//        }

        return $this->fetch();
    }

    //申请讲师
    public function applyLecturer()
    {

        $data = input('post.');

        $data['create_time'] = time();
        $data['uid'] = $this->uid;

        $res = Db::name('lecturer')->insert($data);

        if ($res) {

            $this->success('申请成功');
        } else {
            $this->error('申请失败');
        }
    }

    //新手帮助
    public function help()
    {
        $list = Db::name('help')->field('id,title')->where(['type' => 1, 'status' => 1])->select();

        $this->assign('list', $list);
        return $this->fetch();
    }

    //帮助详情
    public function details($id)
    {

        $info = Db::name('help')->where('id', $id)->find();

        $this->assign('info', $info);
        return $this->fetch();
    }

    //记录帮助查看数量
    public function helpHits(){

        $id = input('id');

        if($id){
            Db::name('help')->where("id",$id)->setInc('hits',1);
        }

        return 'ok';
    }

    //点击记录
    public function clickRecord()
    {

        $aid = input('aid');

        $row = Db::name('advert_look_record')->where(['uid' => $this->uid, 'aid' => $aid])->find();

        if ($row) {

            Db::name('advert')->where('id', $aid)->setInc('hits', 1);
        } else {
            Db::name('advert')->where('id', $aid)->inc('hits')->inc('people_hits')->update();

            $addData = [
                'aid' => $aid,
                'uid' => $this->uid,
                'ip' => ip2long(request()->ip())
            ];

            Db::name('advert_look_record')->insert($addData);

        }

        return 'ok';
    }
}
